package dry.example.study;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author zc
 */
public class ImageDemo {

    private static final String FILE_NAME_RAW = "d:\\raw.jpg";

    private static final String FILE_NAME_LOGO = "d:\\logo.png";

    private static final String FILE_NAME_SAVE = "d:\\save.jpg";

    public static void demo01() {
        File f = new File(FILE_NAME_RAW);
        System.out.println("文件大小(字节)=" + f.length());
    }

    public static void demo02() {
        File f = new File(FILE_NAME_RAW);
        try {
            BufferedImage bi = ImageIO.read(f);
            if (bi == null) {
                System.out.println("无法读取图片文件");
                return;
            }
            System.out.printf("图片宽度=%d 图片高度=%d%n", bi.getWidth(), bi.getHeight());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void demo03() {
        File f = new File(FILE_NAME_RAW);
        try {
            BufferedImage bi = ImageIO.read(f);
            ImageIO.write(bi, "jpg", new File(FILE_NAME_SAVE));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void demo04() {
        File raw = new File(FILE_NAME_RAW);
        File logo = new File(FILE_NAME_LOGO);
        try {
            BufferedImage biRaw = ImageIO.read(raw);
            BufferedImage biLogo = ImageIO.read(logo);
            Graphics2D g = biRaw.createGraphics();
            //对象,水平偏移,垂直偏移,宽度,高度,null(偏移从左上角开始，宽度、高度如果不设置为对象的尺寸则会强制拉伸)
            g.drawImage(biLogo, 100, 200, biLogo.getWidth(), biLogo.getHeight(), null);
            g.dispose();
            ImageIO.write(biRaw, "jpg", new File(FILE_NAME_SAVE));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void demo05() {
        File raw = new File(FILE_NAME_RAW);
        File save = new File(FILE_NAME_SAVE);
        String text = "大家端午节快乐";
        try {
            BufferedImage biRaw = ImageIO.read(raw);
            int width = biRaw.getWidth();
            int height = biRaw.getHeight();
            Graphics2D g = biRaw.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setFont(new Font("宋体", Font.BOLD, 60));
            g.setColor(Color.WHITE);
            FontMetrics fm = g.getFontMetrics();
            int textWidth = fm.stringWidth(text);
            int textHeight = fm.getHeight();
            int x = (width - textWidth) / 2;
            int y = (height - textHeight) / 2;
            int ascent = fm.getAscent();
            int descent = fm.getDescent();
            int leading = fm.getLeading();
            g.drawString(text, x, y);
            g.dispose();
            ImageIO.write(biRaw, "jpg", save);
            System.out.printf("width=%d height=%d textWidth=%d textHeight=%d x=%d y=%d%n", width, height, textWidth, textHeight, x, y);
            System.out.printf("ascent=%d descent=%d leading=%d%n", ascent, descent, leading);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static TextInfo getTextInfo(Font font, String text) {
        TextInfo r = new TextInfo();
        BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics();
        r.setWidth(fm.stringWidth(text));
        r.setHeight(fm.getAscent() + fm.getDescent());
        r.setMaxHeight(fm.getHeight());
        r.setAscent(fm.getAscent());
        r.setDescent(fm.getDescent());
        r.setLeading(fm.getLeading());
        g.dispose();
        return r;
    }

    public static void demo06() {
        String text = "大家端午节快乐";
        Font font = new Font("宋体", Font.BOLD, 60);
        TextInfo ti = getTextInfo(font, text);
        BufferedImage bi = new BufferedImage(ti.getWidth(), ti.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setFont(font);
        g.setColor(Color.GREEN);
        g.drawString(text, 0, ti.getAscent());
        g.dispose();
        try {
            File file = new File(FILE_NAME_SAVE);
            ImageIO.write(bi, "jpg", file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //demo01();
        //demo02();
        //demo03();
        //demo04();
        //demo05();
        demo06();
    }

}